using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace cs2335.lab5
{
    public class RadioButton: IComponent
    {
        #region Variables
        protected Rectangle container = new Rectangle(0, 0, 100, 25);
        protected StringFormat labelFormat = new StringFormat(StringFormatFlags.NoClip);
        protected SolidBrush backColor = new SolidBrush(Color.FloralWhite);
        protected Font font = new Font("SansSerif", 10);
        protected CCTForm owner;
        protected bool hasFocus = false;
        protected bool hidden = false;
        protected string name;
        protected string text;

        protected Rectangle button;
        protected int buttonWidth = 0;
        protected bool selected = false;
        #endregion

        #region Properties
        /// <summary>
        /// Tells whether or not a component is hidden. If it is hidden,
        /// it should not be receiving actions.
        /// </summary>
        public bool Hidden
        {
            get { return hidden; }
        }

        /// <summary>
        /// Stores the width and height of the component
        /// </summary>
        public Size Size
        {
            get { return container.Size; }
            set { container.Size = value; Invalidate(); }
        }

        public Point Location
        {
            get { return container.Location; }
            set
            {
                container.Location = value;
                SetSubComponents();
                Invalidate();
            }
        }

        public Rectangle Bounds
        {
            get { return container; }
            set
            {
                container = value;
                SetSubComponents();
                Invalidate();
            }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public System.Drawing.Font Font
        {
            get { return font; }
            set { font = value; Invalidate(); }
        }

        public string Text
        {
            get { return text; }
            set { text = value; Invalidate(); }
        }

        public CCTForm Owner
        {
            get { return owner; }
            set { owner = value; }
        }

        public Color BackColor
        {
            get { return backColor.Color; }
            set { backColor.Color = value; Invalidate(); }
        }
        #endregion

        #region Events
        public event EventHandler Click;
        public event EventHandler GotFocus;
        public event EventHandler KeyPress;
        public event EventHandler KeyDown;
        public event EventHandler LostFocus;
        public event EventHandler Paint;
        #endregion

        #region IComponent Members
        public void Invalidate()
        {
            if (owner != null)
            {
                owner.Invalidate();
            }
        }

        public void Hide()
        {
            hidden = true;
            Invalidate();
        }

        public void Show()
        {
            hidden = false;
            Invalidate();
        }

        public void OnPaint(PaintEventArgs e)
        {
            if (!hidden)
            {
                Graphics g = e.Graphics;
                int margin = 5;
                if (!selected)
                {
                    g.FillEllipse(new SolidBrush(Color.PaleGoldenrod), button);
                    g.DrawEllipse(Pens.DarkGoldenrod, button);
                }
                else
                {
                    g.DrawEllipse(Pens.DarkGoldenrod, button);
                    Rectangle filler = button;
                    filler.Width = (int) (filler.Width * .75);
                    filler.Height = (int) (filler.Height * .75);
                    int fillMargin = (button.Width - filler.Width) / 2;
                    filler.X += fillMargin;
                    filler.Y += fillMargin;
                    g.FillEllipse(new SolidBrush(Color.Salmon), filler);
                }
                g.DrawString(text, font, Brushes.Black, 
                    new PointF(container.X + ButtonWidth + margin, button.Y + button.Height/2 - font.Size));

                if (hasFocus)
                {
                    //DrawFocusBorder(g);
                }

                if (Paint != null)
                {
                    Paint(this, e);
                }
            }

        }

        public void OnGotFocus(EventArgs e)
        {
            hasFocus = true;
            if (GotFocus != null)
            {
                GotFocus(this, e);
            }
            owner.Invalidate();
        }

        public void OnLostFocus(EventArgs e)
        {
            hasFocus = false;
            if (LostFocus != null)
            {
                LostFocus(this, e);
            }
            owner.Invalidate();
        }

        public void OnMouseClick(System.Windows.Forms.MouseEventArgs e)
        {
            /*
            if (button.Contains(e.Location))
            {
                selected = !selected;
                Invalidate();
            }
             */
            if (Click != null)
            {
                Click(this, e);
            }
        }

        public void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            /* ignore */
        }

        public void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            /* ignore */
        }

        public void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            /* ignore */
        }

        public void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            //Invokes a Click event if the user hits ENTER
            //  while this component has focus
            if (e.KeyChar == '\r')
            {
                if (Click != null)
                {
                    Click(this, e);
                }
            }

            if (KeyPress != null)
            {
                KeyPress(this, e);
            }
        }

        public void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            if (KeyDown != null)
            {
                KeyDown(this, e);
            }
        }
        #endregion

        public int ButtonWidth
        {
            get { return buttonWidth; }
            set { buttonWidth = value + (int)Pens.White.Width; }
        }

        public bool Selected
        {
            get { return selected; }
            set { selected = value; }
        }

        public RadioButton()
        {
            SetSubComponents();
        }

        private void SetSubComponents()
        {
            int margin = 2;
            buttonWidth = container.Height - (margin * 2);

            button = new Rectangle(container.X + margin, container.Y + ((container.Height - buttonWidth)/2), 
                buttonWidth, buttonWidth);
        }
    }
}
